package com.etc.sfts.controller;

import com.etc.sfts.biz.CinemaBiz;
import com.etc.sfts.biz.MovieBiz;
import com.etc.sfts.biz.PublicBiz;
import com.etc.sfts.biz.ipml.CinemaBizImpl;
import com.etc.sfts.biz.ipml.MovieBizImpl;
import com.etc.sfts.biz.ipml.PublicBizImpl;
import com.etc.sfts.dto.PageDto;
import com.etc.sfts.entity.*;
import com.google.gson.Gson;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@Controller
public class DoPublicMassageController {
    @Autowired
    private PublicBiz biz;
    @Autowired
    private MovieBiz movieBiz;
    @Autowired
    private CinemaBiz cinemaBiz;

    public MovieBiz getMovieBiz() {
        return movieBiz;
    }

    public void setMovieBiz(MovieBiz movieBiz) {
        this.movieBiz = movieBiz;
    }

    public CinemaBiz getCinemaBiz() {
        return cinemaBiz;
    }

    public void setCinemaBiz(CinemaBiz cinemaBiz) {
        this.cinemaBiz = cinemaBiz;
    }

    public PublicBiz getBiz() {
        return biz;
    }

    public void setBiz(PublicBiz biz) {
        this.biz = biz;
    }
    //获得所有的发布信息
    @RequestMapping("/getAllPublic")
    public  void getAllPublic(HttpServletRequest request, HttpServletResponse response)throws Exception{
        Integer page = 1;
        Integer size = 5;

        try {
            //閿熷彨璁规嫹椤甸敓鏂ゆ嫹閿熻鍑ゆ嫹涓洪敓鏂ゆ嫹
            if (request.getParameter("page") != null) {
                //閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹杞敓鏂ゆ嫹
                page = Integer.valueOf(request.getParameter("page"));
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        try {
            //閿熷彨璁规嫹閿熸枻鎷烽敓鏂ゆ嫹閿熻鍑ゆ嫹涓洪敓鏂ゆ嫹
            if (request.getParameter("size") != null) {
                //閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹杞敓鏂ゆ嫹
                size = Integer.valueOf(request.getParameter("size"));
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        // 閿熸枻鎷穝ize閿熸枻鎷穚age閿熸枻鎷烽敓鏂ゆ嫹閿熸嵎杈炬嫹
        size = size <= 0 ? 5 : size;
        page = page <= 0 ? 1 : page;
        Long count=biz.queryPublicCount();
        //閿熸枻鎷峰彇閿熸枻鎷烽敓鎻鎷烽敓锟�
        Integer maxPage = (int) (count % size == 0 ? (count / size) : (count / size + 1));
        // 閿熺殕纰夋嫹鍓嶉〉閿熸枻鎷烽敓鏂ゆ嫹閿熸嵎杈炬嫹
        page = page > maxPage ? maxPage : page;
        List<Public> list=biz.getPublicMessage(size, (page-1)*size);
        //閿熸枻鎷烽敓鏂ゆ嫹閿熸彮绛规嫹閿熸枻鎷烽敓鏂ゆ嫹閿熻鑰欑鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷疯閿熸枻鎷蜂竴閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷�
        PageDto dto=new PageDto();

        dto.setMaxPage(maxPage);
        dto.setOlist(list);
        dto.setPage(page);
        dto.setSize(size);
        System.out.println(list);

        //閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷锋伅閿熸枻鎷烽敓鏂ゆ嫹
        Message m=new Message();
        m.setFlag(1);
        m.setMsgcontent(dto);
        //閿熸枻鎷烽敓鏂ゆ嫹鎭浆閿熸枻鎷烽敓鏂ゆ嫹json閿熻鍑ゆ嫹閿熸枻鎷�
        Gson json=new Gson();
        String str=json.toJson(m);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        //閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷峰簲
        out.print(str);
        out.flush();
        out.close();
    }
    //获得发布信息总数
    @RequestMapping("/getPublicCount")
    public void getPublicCount(HttpServletResponse response)throws Exception{
        Long count=biz.queryPublicCount();
        PrintWriter out=response.getWriter();
        out.print(count);
        out.flush();
        out.close();

    }
    //删除发布信息
    @RequestMapping("/deletePublicMassage")
    public  void deletePublicMassage(HttpServletResponse response,HttpServletRequest request)throws Exception{
        String pid=request.getParameter("publicid");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        try {
            Integer id=Integer.valueOf(pid);
            if(biz.deletePublicMassage(id)) {
                out.print("<script>alert('删除成功！');location.href='publicmassagelist.jsp';</script>");
            }else {
                out.print("<script>alert('删除失败！');histroy.go(-1);</script>");
            }
        } catch (Exception e) {
            out.print("<script>alert('参数异常，删除失败！');histroy.go(-1);</script>");
        }
        out.flush();
        out.close();

    }
    @RequestMapping("/addPublic")
    public  void addPublic(HttpServletRequest request,HttpServletResponse response)throws Exception{
        // 影院名称
        String name = request.getParameter("name");
        // 影院地区
        String place = request.getParameter("place");
        // 电影名字
        String moviename = request.getParameter("moviename");
        // 放映时间
        String time = request.getParameter("time");
        // 放映日期
        String date = request.getParameter("date");
        // 电影语言类型
        String language = request.getParameter("language");
        // 电影票价格
        Double pirce = Double.parseDouble(request.getParameter("price"));
        // 影院地址
        String address = request.getParameter("address");
        // 电影厅号码
        String movieaddress = request.getParameter("movieaddress");
        // 影院id
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        //通过电影名字查询电影

        Movie movie = movieBiz.getMovieByName(moviename);
        //通过影院名字查询影院


        //获取登陆的管理员
        Manager manager = (Manager) request.getSession().getAttribute("Manager");
        Cinema cinema = cinemaBiz.getCinemaByName(name);
        //把获取的数据封装在一个类中
        Public p = new Public();
        if(movie==null) {
            out.print("<script>alert('电影名字有误');history.go(-1);</script>");
        }else if(cinema==null){
            out.print("<script>alert('影院名字有误');history.go(-1);</script>");

        }else {
            p.setPublish_mid(movie.getMovie_id());
            p.setPublish_cid(cinema.getCinema_id());
            p.setManager_id(manager.getManager_id());
            p.setPublish_address(place);
            p.setPublish_cinema(name);
            p.setPublish_date(date);
            p.setPublish_time(time);
            p.setPublish_movie(moviename);
            System.out.println(moviename);
            p.setPublish_deaddress(address);
            p.setPublish_room(movieaddress);
            p.setPublish_language(language);
            p.setPublish_price(pirce);
            PublicBiz biz = new PublicBizImpl();

            //判断是否满足条件
            if (biz.insertPublic(p)) {
                out.print("<script>alert('添加失败');history.go(-1);</script>");
            } else {
                out.print("<script>alert('添加成功');location.href='publicmassagelist.jsp';</script>");
            }
        }


        out.flush();
        out.close();
    }
    //通过管理员id获得影院
    @RequestMapping("/getCinemaByManageId")
    public void getCinemaByManageId(HttpServletResponse response,HttpServletRequest request)throws Exception{
        //获取登陆的管理员
        Manager manager = (Manager) request.getSession().getAttribute("Manager");
        List<Cinema> cinema =cinemaBiz.queryCinemaByManagerId(manager.getManager_id());
        Message m=new Message();


        if(cinema.size()>0&&cinema!=null) {
            m.setFlag(1);
            m.setMsgcontent(cinema);
        }else {
            m.setFlag(0);
        }

        Gson json=new Gson();
        String str=json.toJson(m);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        out.print(str);
        out.flush();
        out.close();

    }

    //导出excel文件
    @RequestMapping("/exportPublicExcel")
    @ResponseBody
    public String exportPublicExcel(HttpServletResponse response, Map<String, Object> params) {

        HSSFWorkbook workbook = biz.exportPublicExcel(params);
        try {
            if (response != null) {
                response.setContentType("application/vnd.ms-excel;charset=utf-8");
                response.setHeader("Content-Disposition",
                        "attachment;filename=\"" + new String(("影院信息" + ".xls").getBytes("gb2312"), "ISO8859-1"));
                OutputStream out = response.getOutputStream();
                workbook.write(out);
                out.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
